/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.crab.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.crab.entity.CouponProduct;
import com.kaaction.modules.crab.service.CouponProductService;

/**
 * 兑换券与商品关系Controller
 * @author huanghaiming
 * @version 2017-07-19
 */
@Controller
@RequestMapping(value = "${adminPath}/crab/couponProduct")
public class CouponProductController extends BaseController {

	@Autowired
	private CouponProductService couponProductService;
	
	@ModelAttribute
	public CouponProduct get(@RequestParam(required=false) String id) {
		CouponProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = couponProductService.get(id);
		}
		if (entity == null){
			entity = new CouponProduct();
		}
		return entity;
	}
	
	@RequiresPermissions("crab:couponProduct:view")
	@RequestMapping(value = {"list", ""})
	public String list(CouponProduct couponProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CouponProduct> page = couponProductService.findPage(new Page<CouponProduct>(request, response), couponProduct); 
		model.addAttribute("page", page);
		return "modules/crab/couponProductList";
	}

	@RequiresPermissions("crab:couponProduct:form")
	@RequestMapping(value = "form")
	public String form(CouponProduct couponProduct, Model model) {
		model.addAttribute("couponProduct", couponProduct);
		return "modules/crab/couponProductForm";
	}

	@RequiresPermissions("crab:couponProduct:save")
	@RequestMapping(value = "save")
	public String save(CouponProduct couponProduct, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, couponProduct)){
			return form(couponProduct, model);
		}
		couponProductService.save(couponProduct);
		addMessage(redirectAttributes, "保存兑换券与商品关系成功");
		return "redirect:"+Global.getAdminPath()+"/crab/couponProduct/?repage";
	}
	
	@RequiresPermissions("crab:couponProduct:delete")
	@RequestMapping(value = "delete")
	public String delete(CouponProduct couponProduct, RedirectAttributes redirectAttributes) {
		couponProductService.delete(couponProduct);
		addMessage(redirectAttributes, "删除兑换券与商品关系成功");
		return "redirect:"+Global.getAdminPath()+"/crab/couponProduct/?repage";
	}

}